package com.hainatong.shiphelper.module.warehouse.adapter;

import java.util.List;

import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ItemShelfChildViewBinding;
import com.hainatong.shiphelper.module.warehouse.entity.WareShelfBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import androidx.databinding.DataBindingUtil;

public class ShelfExpandableAdapter extends BaseExpandableListAdapter
{

    private Context mContext;

    private List<WareShelfBean> wareShelfBeans;


    public ShelfExpandableAdapter(Context mContext)
    {
        this.mContext = mContext;
    }


    public ShelfExpandableAdapter(Context mContext, List<WareShelfBean> shelfBeans)
    {
        this.mContext = mContext;
        this.wareShelfBeans = shelfBeans;
    }


    public List<WareShelfBean> getWareShelfBeans()
    {
        return wareShelfBeans;
    }

    public void setWareShelfBeans(List<WareShelfBean> wareShelfBeans)
    {
        this.wareShelfBeans = wareShelfBeans;
        this.notifyDataSetChanged();
    }

    @Override
    public int getGroupCount()
    {
        //避免出现数据空 导致奔溃的问题;
        return wareShelfBeans!=null?wareShelfBeans.size():0;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return (wareShelfBeans!=null && wareShelfBeans.get(groupPosition).getChildren()!=null)?wareShelfBeans.get(groupPosition).getChildren().size():0;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return wareShelfBeans.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return wareShelfBeans.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public boolean hasStableIds()
    {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        WareShelfBean shelfBean = (WareShelfBean) getGroup(groupPosition);
        //View view = LayoutInflater.from(mContext).inflate(R.layout.item_department_view,null);
        ItemShelfChildViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_shelf_child_view, null,false);
        binding.ivArrowTag.setVisibility( View.VISIBLE );

        if(isExpanded)
        {
            binding.ivArrowTag.setImageResource( R.mipmap.ic_arrow_down );
        }
        else
        {
            binding.ivArrowTag.setImageResource( R.mipmap.ic_arrow_right );
        }
        binding.tvShelfName.setText( shelfBean.getShelfName() );
        binding.tvCount.setText( shelfBean.getFloors() );

        return binding.getRoot();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        WareShelfBean.ChildrenBean childrenBean = (WareShelfBean.ChildrenBean) getChild(groupPosition,childPosition);
        //View view = LayoutInflater.from(mContext).inflate(R.layout.item_department_view,null);
        ItemShelfChildViewBinding chlidBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_shelf_child_view, null,false);
        chlidBinding.tvShelfName.setText( childrenBean.getCode() );
        chlidBinding.ivArrowTag.setVisibility(View.INVISIBLE);
        chlidBinding.tvCount.setVisibility(View.INVISIBLE);
        return chlidBinding.getRoot();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

}
